/*
查找字符串的while do..while 模型
使用到的函数 strstr(p,"abc")
从在字符串首地址p开始匹配abc字符,并返回其首地址
核心思想:对地址加查字符串字节数,使其能够实现功能

*/
# include<stdio.h>
# include<string.h>

//使用do...while循环实现
int main1 (void)
{
	char *p = "abchgkhnkjabchasghabcaghabcagjkabc";
	int num = 0;
	do
	{
	if ( (p = strstr(p,"abc"))!= 0)
	{
		num++;
		p += strlen("abc");
	}
	else
		break;
	}while(*p != 0);
	printf ("%d",num);
	return 0;
}

//使用while循环实现
int main2 (void)
{
	char *p = "abchgkhnkjabchasghabcaghabcagjkabc";
	int num = 0;
	while ((p = strstr(p,"abc"))!= 0)
	{
		num++;
		p += strlen("abc");
		if(*p == 0)
			break;
	}
	printf ("%d",num);

	return 0;}
void get_num(char *p,int *num)
{
	int n = 0;//記住要初始化
	char * tmp = NULL;
	tmp = p;
	while ((tmp = strstr(tmp,"abc"))!= 0)
	{
		tmp += strlen ("abc");
		n ++;
		if (*tmp == 0)
			break;
	}
	*num = n;
}
// 使用函数实现
int main (void)
{
	char *p = "abchgkhnkjabchasghabcaghabcagjkabc";
	int num = 0;
	get_num(p,&num);
	printf ("%d",num);

	return 0;
	}
